package com.phoxell.xync;

import com.phoxell.Agent;
import com.phoxell.Serial;
import java.io.IOException;
import java.util.logging.Logger;

import javax.servlet.http.*;

@SuppressWarnings("serial")
public class UpgradeServlet extends HttpServlet{
	private static final Logger log = Logger.getLogger(UpgradeServlet.class.getName());
	public void doGet(HttpServletRequest req, HttpServletResponse rsp)
			throws IOException{
		try{
			log.warning("try");
			Agent a=new Agent(req.getHeader("User-Agent"));
			log.warning(a.os);
			log.warning(a.serial);
			log.warning(a.version);
			log.warning(a.firmware+"");
			Serial i=new Serial(a.serial);
			int f=(int)i.getFlags();
			log.warning("enter");
			if(f==2){
				String v=a.version;
				if(v.equals("0.4")){
					rsp.addHeader("Version","0.5");
					rsp.addHeader("Checksum","8371759");
					rsp.sendRedirect("/downloads/v05/xync.exe");
				}else{
					rsp.addHeader("Version","0.4");
					rsp.addHeader("Checksum","8371759");
					rsp.sendRedirect("/downloads/v04/xync.exe");
				}
				log.warning("return");
				return;
			}
			log.warning("leave");
		}catch(Exception e){
			log.warning("catch");
			log.info(e.getClass().getName());
			log.info(e.getMessage());
			//e.printStackTrace();
		}
		rsp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
	}
}